from django.shortcuts import render
from BASE.interface import get_backend_data as backend


# Create your views here.
class Novels_Views:
    __novels_index_html = "Novels/novels_index.html"
    __novels_cover_html = "Novels/novels_cover.html"
    __readingRoom_html = "Novels/readingRoom.html"

    def novels_index_view(self, request):
        novels = self.__get_novels_data(form_data={
            "function": "Showall"
        })
        read = self.__get_novels_data(form_data={
            "function": "Showrank",
            "rank": "read",
        })
        update = self.__get_novels_data(form_data={
            "function": "Showupdate",
        })
        collection = self.__get_novels_data(form_data={
            "function": "Showrank",
            "rank": "collection",
        })
        return render(request, self.__novels_index_html, context={
            "novels": novels,
            "read": read,
            "update": update,
            "collection": collection
        })
        pass

    def novels_cover_view(self, request, fId):
        session = None
        try:
            session = request.COOKIES['session']
        except KeyError:
            session = ''
        novel = self.__get_novels_data(
            form_data={
                "function": "Showfic",
                "session": session,
                "fId": fId
            }
        )
        return render(request, self.__novels_cover_html, context={
            "novel": novel
        })
        pass

    def readingRoom(self, request, fId, chapter):
        session = None
        try:
            session = request.COOKIES['session']
        except KeyError:
            session = ''
        novel = self.__get_novels_data(form_data={
            "function": "Showfic",
            "session": session,
            "fId": fId
        })
        return render(request, self.__readingRoom_html, context={
            "novel": novel,

        })
        pass

    def __get_novels_data(self, url="http://193.112.72.237/Web-comfun/web/index.php?r=fiction/index", form_data=None):
        return backend.get_novels_data(url=url, form_data=form_data)
        pass


novels_views = Novels_Views()
